//
//  EventContentView.m
//  CJMobile
//
//  Created by wh15113030 on 16/4/5.
//  Copyright © 2016年 长江证券. All rights reserved.
//

#define WIDTH_SCALE1  (KScreenWidth / 320.0)


#define backView_height 33
#define padding 10
#define Radius 16

#define backView_width1 72.5 * WIDTH_SCALE1
#define backView_width2 45 * WIDTH_SCALE1
#define backView_width3 60 * WIDTH_SCALE1
#define pading 6 * WIDTH_SCALE1


#import "EventContentView.h"
#import "UIView+Extension.h"

@interface EventContentView ()<UITextViewDelegate>


@property (nonatomic, strong) UIView *backView;

//按钮的颜色
//@property (nonatomic, strong) UIColor *btnColor;
@property (nonatomic, strong) NSArray *btnColorArr;

//按钮的文字
@property (nonatomic, strong) NSArray *textArr;

//占位文字
@property (nonatomic, copy) NSString *placeholder;

@property (nonatomic, copy) NSString *text;

@property (nonatomic, assign) NSInteger tag1;

@property (nonatomic, strong) UIButton *selectedBtn;

@property (nonatomic, assign) NSInteger selectIndex;


@end


@implementation EventContentView

- (instancetype)initWithFrame:(CGRect)frame textArr:(NSArray *)textArr btnColorArr:(NSArray *)btnColorArr palcerHoler:(NSString *)palceholer text:(NSString *)text tag:(NSInteger)tag selectIndex:(NSInteger)selectIndex {
    self = [super initWithFrame:frame];
    if (self) {

        self.selectIndex = selectIndex;
        
        self.tag1 = tag;
        self.btnColorArr = btnColorArr;
        
        self.textArr = textArr;
        
        self.placeholder = palceholer;
        
        self.text = text;
        
        //添加视图
        [self addView];
        
    }
    
    return self;

}

+ (EventContentView *)getContentView{
    return [[super alloc] init];
}


//添加视图
- (void)addView{
    //添加视图
//    [self addSubview:self.textView];
    UIButton *button0 = [self setupButton:self.backView buttonWidth:backView_width1 padd:0 tag:0 MasonryButtonWidth:0];
    
     UIButton *button1 = [self setupButton:button0 buttonWidth:backView_width2 padd:pading tag:1 MasonryButtonWidth:backView_width1];

     UIButton *button2 = [self setupButton:button1 buttonWidth:backView_width2 padd:pading tag:2 MasonryButtonWidth:backView_width2];

     UIButton *button3 = [self setupButton:button2 buttonWidth:backView_width2 padd:pading tag:3 MasonryButtonWidth:backView_width2];

    [self setupButton:button3 buttonWidth:backView_width3 padd:pading tag:4 MasonryButtonWidth:backView_width2];
    
}

- (UIButton *)setupButton:(UIView *)MasonryButton buttonWidth:(CGFloat)buttonWidth padd:(CGFloat)pad tag:(NSInteger )tag2 MasonryButtonWidth:(CGFloat)masonryButtonWidth{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.tag = tag2;
    button.layer.cornerRadius = Radius;
    button.clipsToBounds = YES;
    
    if (self.selectIndex == tag2) {
        button.selected = YES;
        button.layer.borderWidth = 0;
    }else{
        button.selected = NO;
        button.layer.borderWidth = 0.5;
        button.layer.borderColor = XRGB(e5, e5, e5).CGColor;
    }
    
    button.backgroundColor = self.btnColorArr[tag2];
    
    
    [button setTitle:self.textArr[tag2] forState:UIControlStateNormal];
    button.titleLabel.font = FONT(15);
    
    [button setTitleColor:XRGB(05, 05, 05) forState:UIControlStateNormal];
    
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
//    NSMutableArray *imagerArr = [self getImage];
//    [button setBackgroundImage:imagerArr[tag2] forState:UIControlStateSelected];
    
    [button addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.backView addSubview:button];
 
    
    return button;
}


-(UIView *)backView{
    if (_backView == nil) {
        _backView = [[UIView alloc] init];
        [self addSubview:_backView];
//        [self getBackViewMasonry:_backView];
        
     }
    return _backView;
}


-(void)clickBtn:(UIButton *)button{
//    self.selectedBtn.selected = NO;
//    button.selected = YES;
//    self.selectedBtn = button;
//    
   NSLog(@"%ld-------%s",(long)button.tag,__func__);
    
    if (self.btnBlock) {
        self.btnBlock(button.tag);
    }
}


-(void)textViewDidEndEditing:(UITextView *)textView{
    if (textView.tag == self.tag1) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"scheduleContentChange" object:nil];
    }
}

- (void)textViewDidChange:(UITextView *)textView {
    if (textView.tag == self.tag1) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"textViewDidChange" object:nil];
    }
    
}

//- (NSMutableArray *)getImage{
//    
//    NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:10];
//    
//    UIImage *image0 = [UIImageUtil imageWithColor:XRGB(7a, c6, f8)];
//    UIImage *image1 = [UIImageUtil imageWithColor:XRGB(9f, cf, 74)];
//    UIImage *image2 = [UIImageUtil imageWithColor:XRGB(f3, c2, 4d)];
//    UIImage *image3 = [UIImageUtil imageWithColor:XRGB(ad, 8b, e7)];
//    UIImage *image4 = [UIImageUtil imageWithColor:XRGB(f3, 80, 4d)];
//    
//    [arr addObject:image0];
//    [arr addObject:image1];
//    [arr addObject:image2];
//    [arr addObject:image3];
//    [arr addObject:image4];
//    
//    return arr;
//}



//
//    for (NSInteger i = 0; i < 5; i ++) {
//
//        UIButton *button;
////        if (i == 0) {
////            button =  [self setupButton:nil buttonWidth:backView_width1 padd:pading tag:];
////        }else if (i == 1){
////           button =  [self setupButton:button buttonWidth:backView_width2 padd:pading];
////        }else if (i == 2){
////           button =  [self setupButton:button buttonWidth:backView_width2 padd:pading];
////        }else if (i == 3){
////           button =  [self setupButton:button buttonWidth:backView_width2 padd:pading];
////        }else if (i == 4){
////           button =  [self setupButton:button buttonWidth:backView_width3 padd:pading];
////        }else{
////
////        }
//
////           NSLog(@"%ld-------%s",(long)self.selectIndex,__func__);
////
////        if (self.selectIndex == i) {
////            button.selected = YES;
////            button.layer.borderWidth = 0;
////        }else{
////            button.selected = NO;
////            button.layer.borderWidth = 0.5;
////            button.layer.borderColor = UIColorFromRGB(e5, e5, e5).CGColor;
////        }
////
////        button.backgroundColor = self.btnColorArr[i];
////
////        button.tag = i;
////
////        [button setTitle:self.textArr[i] forState:UIControlStateNormal];
////        button.titleLabel.font = FONT(15);
////
////        [button setTitleColor:UIColorFromRGB(05, 05, 05) forState:UIControlStateNormal];
////
////        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
//////        [button setBackgroundImage:imagerArr[i] forState:UIControlStateSelected];
////
////        [button addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
////
////        [self.backView addSubview:button];
//
////
////        //宽
////        CGFloat buttn_width;
////
////        //x
////        if (i == 0) {
////            buttn_width = 73;
////            button.frame = CGRectMake(0, 0, buttn_width, backView_height);
////        }else if (i == 4){
////            buttn_width = 60;
////            CGFloat x = 95 + 50 * 3 + 3  * 6;
////            button.frame = CGRectMake(x, 0, buttn_width, backView_height);
////        }else{
////            buttn_width = 50;
////            CGFloat x = 35 + i * (buttn_width+ 6);
////            button.frame = CGRectMake(x, 0, buttn_width, backView_height);
////        }
//    }



@end
